package container

import (
	"codejy/store"
	"codejy/structdef"
	"codejy/typedefault"
	"strconv"
)

type Void struct{}
type TypeSet struct {
	Addr        string
	ThreadStore *structdef.ThreadStore
	Set         map[string]Void
	SetIndex    []string
}

// 创建实例
func (a *TypeSet) CreateTypeDefault(addr string, threadStore *structdef.ThreadStore, parameter ...string) {
	set := &TypeSet{
		Addr:        addr,
		ThreadStore: threadStore,
		Set:         make(map[string]Void, 20),
		SetIndex:    []string{},
	}
	store.AddTypeDefaultInstanceStore(threadStore, a.GetTypeName(), addr, set)
}
func (a *TypeSet) GetData() interface{} {
	return a.SetIndex
}

func (a *TypeSet) ToStr() string {
	var Str string
	//如果是Set,那么打印出来
	value2, b2 := typedefault.GetTypeDefaultValue[[]string](a.ThreadStore, a.GetTypeName(), a.Addr)
	if b2 {
		Str = "["
		//以逗号分隔
		for _, v := range value2 {
			Str += v + ","
		}
		Str = Str[:len(Str)-1]
		Str += "]"
		return a.GetTypeName() + ":" + Str
	}
	return ""
}

func (a *TypeSet) ParameterProcessing(parameter []string) []string {
	return parameter
}

func (a *TypeSet) SetAddr(addr string) {
	a.Addr = addr
}

func (a *TypeSet) SetThreadStore(threadStore *structdef.ThreadStore) {
	a.ThreadStore = threadStore
}

// 添加数据
func (a *TypeSet) AddSet(value string) {
	_, p := a.Set[value]
	if p { //已经存在
		return
	}
	a.Set[value] = Void{}
	a.SetIndex = append(a.SetIndex, value)
}

// 通过下标修改数据
func (a *TypeSet) UpdateSet(index string, value string) {
	atoi, _ := strconv.Atoi(index)
	if atoi < 0 || atoi >= len(a.SetIndex) {
		panic("index out of range")
	}
	//删除旧数据
	delete(a.Set, a.SetIndex[atoi])
	//添加新数据
	a.SetIndex[atoi] = value
	a.Set[value] = Void{}
}

// 通过下标删除数据
func (a *TypeSet) DeleteSet(index string) {

	atoi, _ := strconv.Atoi(index)
	if atoi < 0 || atoi >= len(a.SetIndex) {
		panic("index out of range")
	}
	delete(a.Set, a.SetIndex[atoi])
	a.SetIndex = append(a.SetIndex[:atoi], a.SetIndex[atoi+1:]...)

}

// 通过下标获取数据
func (a *TypeSet) GetSet(index string) string {

	atoi, _ := strconv.Atoi(index)
	if atoi < 0 || atoi >= len(a.SetIndex) {
		panic("index out of range")
	}
	return a.SetIndex[atoi]

}

// 获取数量
func (a *TypeSet) GetSetCount() string {

	return strconv.Itoa(len(a.SetIndex))
}

// Set转换为List
func (a *TypeSet) ToList() string {

	return SliceToList(a.SetIndex, a.ThreadStore)
}

func (a *TypeSet) GetDef() string {
	return `
		@CodeManual(def="new("Set")",des="创建Set容器")
		type  Set{
			def init{
				core&CreateTypeDefault()
			}

			@CodeManual(def="add($1)",des="添加数据,例:set.add("1")")
			def add{
				core&AddSet($1)
            }
		
			@CodeManual(def="set($1,$2)",des="修改数据,例:set1.set(1,"2")")
			def set{
				core&UpdateSet($1,$2)
            }
			
			@CodeManual(def="del($1)",des="删除数据,例:set1.del(1)")
			def del{
				core&DeleteSet($1)	
			}
			//获取数据
			//setDemo.get("1")
			@CodeManual(def="get($1)",des="获取数据,例:set1.get(1)")
			def get{
				to core&GetSet($1)	
			}
			//获取数量
			@CodeManual(def="size()",des="获取数量,例:set1.size()")
			def size{
				to core&GetSetCount()
			}
			//Set转换为List
			//setDemo.toList()
			def toList{
				to core&ToList()
			}	

			def toStr{
				to core&ToStr()
			}
		}
	`
}

func init() {
	typedefault.RegisterTypeDefault(&TypeSet{})
}

func (a *TypeSet) GetTypeName() string {
	return "Set"
}
